<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\common\service\SysLogService;


class User extends Controller
{
    public function getUserList()
    {
        $data = request()->post();
        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;
        
        $map = [];
        $map['is_delete'] = 0;

        
        if (isset($data['name']) && !empty($data['name'])) {
            $map['name'] = ['=', $data['name']];
        }
        


        if (isset($data['account']) && $data['account'] !== '') {
            $map['account'] =  array("=", $data['account']);
        }

        if (isset($data['status']) && $data['status'] !== '') {
            $map['status'] =  array("=", $data['status']);
        }

 

        // 获取总数
        $count = Db::table('zstp_user')
            ->where($map)
            ->count();
            
        // 获取分页数据
        $list = Db::table('zstp_user')
            ->where($map)
            ->page($page, $limit)
            ->order('create_time desc')
            ->select();
            
        return json([
            'code' => 200,
            'message' => '获取成功',
            'data' => [
                'totalCount' => $count,
                'resultList' => $list,
                'page' => $page,
                'limit' => $limit
            ]
        ]);
    }

    public function updateUserStatus(){
        $data = request()->post();
        $id = $data['id'];
        $status = $data['status'];
        //更新zstp_user表的status字段
        $user = Db::table('zstp_user')->where('id', $id)->update(['status' => $status]);

        // 操作日志
        
        if($status == 1){
            $statusText = '启用';
        }else{
            $statusText = '禁用';
        }

        // 获取用户信息

        $userInfo = Db::table('zstp_user')->where('id', $id)->find();
        $account= $userInfo['account'];

        $sysLogService = new SysLogService();
        $result =  $sysLogService->addLog('更新账号(”'.$account.'“为'.$statusText.'状态)', $data['id'], 2, '账号id:'.$data['id']);
   
        return json([
            'code' => 200,
            'message' => '更新成功',
            'data' => $user
        ]);

    }

    public function deleteuser(){
        $data = request()->post();
        $id = $data['id'];
        //删除zstp_user表的记录
        $user = Db::table('zstp_user')->where('id', $id)->update(['is_delete' => 1]);
        $userInfo = Db::table('zstp_user')->where('id', $id)->find();
        $account= $userInfo['account'];

        // 操作日志
        $sysLogService = new SysLogService();
        $result =  $sysLogService->addLog('删除账号(”'.$account.'“)', $data['id'], 3, '账号id:'.$data['id']);


        return json([
            'code' => 200,
            'message' => '删除成功',
            'data' => $user
        ]);
    }

    public function adduser(){

        /**
         * 
         * 前端参数
         * {"name":"张三","account":"zhangsan","password":"123456","status":"1","roles":"user","remarks":"备注"}
         * 
         */

        $data = request()->post();
        $name = $data['name'];
        $account = $data['account'];
        $password = $data['password'];
        $status = $data['status'];
        $roles = $data['roles'];
        $remarks = $data['remarks'];

        //判断账号是否存在

        $map['account'] = $account;
        $map['is_delete'] = 0;

        $user = Db::table('zstp_user')->where($map)->find();
        if($user){
            return json([
                'code' => 200,
                'message' => '账号已存在'
            ]);
        }   

        //判断密码是否符合要求
        if(strlen($password) < 6){
            return json([
                'code' => 200,
                'message' => '密码不符合要求'
            ]);
        }

        //判断角色是否符合要求
        if($roles != 'user' && $roles != 'admin'){
            return json([
                'code' => 200,
                'message' => '角色不符合要求'
            ]);
        }
        
        //添加用户
        $id = Db::table('zstp_user')->insertGetId([
            'name' => $name,
            'account' => $account,
            'password' =>  md5($password),
            'status' => $status,
            'roles' => $roles,
            'remarks' => $remarks
        ]);
        if($id){
            // 操作日志
            $sysLogService = new SysLogService();
            $result =  $sysLogService->addLog('添加账号(”'.$account.'“)', $id, 1, '账号id:'.$id);
            return json([
                'code' => 200,
                'message' => '添加成功' 
            ]);
        }
    }


    public function updateUser(){
        $data = request()->post();
        $id = $data['id'];
        $name = $data['name'];
        $account = $data['account'];
       
        $status = $data['status'];
        $roles = $data['roles'];
        $remarks = $data['remarks'];

        $data = [];

        if(isset($name) && !empty($name)){
            $data['name'] = $name;
        }

        if(isset($account) && !empty($account)){
            $data['account'] = $account;
        }

        if(isset($data['password']) && !empty($data['password'])){
            $data['password'] = md5($data['password']);
        }

        $data['status'] = $status;

        if(isset($roles) && !empty($roles)){
            $data['roles'] = $roles;
        }

        if(isset($remarks) && !empty($remarks)){
            $data['remarks'] = $remarks;
        }


        $map['id'] = $id;
        
        $user = Db::table('zstp_user')->where($map)->update($data);

        // 操作日志
        $sysLogService = new SysLogService();
        $result =  $sysLogService->addLog('更新账号(”'.$account.'“)', $id, 2, "更新内容:".json_encode($data,JSON_UNESCAPED_UNICODE));
       
            return json([
                'code' => 200,
                'message' => '更新成功'
            ]);
        
    }



}